<?php
	include('../init.php');
	if(!isset($_SESSION['m_id'])){
		
	}else{
			$m_id = $_SESSION['m_id'];
			
			$data['msg'] = '';
			$data['status'] = 'fail';
			
			$sql = "SELECT * FROM hs_cart WHERE m_id = '$m_id'";
			$result = mysql_query($sql);
			if(mysql_num_rows($result)==0) {
				$data['msg'] .= 'ยังไม่มีสินค้าในรถเข็น';
			}else{
				$grand_total = 0;
				$pq = $_POST['pq'];
				$service_product = array();
				$sp = 0;
				$mailProductTable =<<<mailProductTable
					<table style="border:1px solid #666666;">
						<tr>
							<th style="border:1px solid #666666;padding:5px 10px;">Type</th>
							<th style="border:1px solid #666666;padding:5px 10px;">Style Number</th>
							<th style="border:1px solid #666666;padding:5px 10px;">Name</th>
							<th style="border:1px solid #666666;padding:5px 10px;">Detail</th>
							<th style="border:1px solid #666666;padding:5px 10px;width:80px;">จำนวน</th>
						<tr>
mailProductTable;
				while($cart = mysql_fetch_array($result)) {
					$sub_total = $cart['p_sell'] * $cart['quantity'];
					
					$q = $pq[$cart['p_id']];
					
					$sql_order_detail[] = <<<order_detail
						INSERT INTO hs_qoute_product (p_id,p_name,quantity,q_id) 
						VALUES('{$cart['p_id']}','{$cart['p_name']}',{$q},
order_detail;
					$sql = "SELECT 	p_style_number , p_type , p_name , p_detail FROM hs_product WHERE p_id ={$cart['p_id']}";
					$query_p = mysql_query($sql);
					$result_p = mysql_fetch_assoc($query_p);
					
					$p_type = '&nbsp;';
					$p_style_number = '&nbsp;';
					$p_name = '&nbsp;';
					$p_detail = '&nbsp;';
					if(trim($result_p['p_type'])!=''){
						$p_type = $result_p['p_type'];
					}
					if(trim($result_p['p_style_number'])!=''){
						$p_style_number = $result_p['p_style_number'];
					}
					if(trim($result_p['p_name'])!=''){
						$p_name = $result_p['p_name'];
					}
					if(trim($result_p['p_detail'])!=''){
						$p_detail = $result_p['p_detail'];
					}
					
					$mailProductTable .=<<<mailProductTable
						<tr>
							<td style="border:1px solid #666666;padding:5px 10px;">{$p_type}</td>
							<td style="border:1px solid #666666;padding:5px 10px;">{$p_style_number}</td>
							<td style="border:1px solid #666666;padding:5px 10px;">{$p_name}</td>
							<td style="border:1px solid #666666;padding:5px 10px;">{$p_detail}</td>
							<td style="border:1px solid #666666;padding:5px 10px;width:80px;text-align:center;">{$q}</td>
						<tr>
mailProductTable;
				}
				
				$time_ = time();
				
				$sql = "INSERT INTO hs_qoute(m_id,q_status,q_dateadd,q_requirement) VALUES('$m_id',0,'$time_','".$_POST['requirement']."')";
				$query = mysql_query($sql);
				if(!$query){ // จัดเก็บคำสั่งซื้อ
					$data['msg'] .= 'alert("เพิ่มเข้าฐานข้อมูล Qoute ไม่ได้");';
				}else{
					$orderid = mysql_insert_id();
					
					//$fp = fopen("test.txt", "w");
					
					foreach ($sql_order_detail as $value) { // จัดเก็บรายการสินค้า
						$sql = $value."'$orderid')";
						mysql_query($sql);
						//fwrite($fp, $sql."\n");
					}
					
					//fclose($fp);
					
					$mail = new PHPMailer();
					$mail->IsMail();
					$mail->CharSet="utf-8";
					$mail->IsHTML(true);
					
					$mail->From = SALE_EMAIL;
					$file = APPPATH.'/mail/qoute.txt';
					$handle = fopen($file,'r');
					if ($handle) {
					    while (!feof($handle)) {
						$file_ = fgets($handle, 4096);
						$body .= $file_."<br>";
					    }
					    fclose($handle);
					}
					$q_email = $db->queryUniqueObject("SELECT m_id , m_name , m_lastname , m_email FROM hs_member WHERE m_id = ".$m_id);
					$body = str_replace('{NAME}',$q_email->m_name,$body);
					$body = str_replace('{LASTNAME}',$q_email->m_lastname,$body);
					$body = str_replace('{ORDER_TABLE}',$mailProductTable,$body);
					$body = str_replace('{SITENAME}',SITENAME,$body);
					$mail->FromName = QOUTE_MAIL_TITLE;
					$mail->AddReplyTo(MAIN_EMAIL, QOUTE_MAIL_TITLE);
					$mail->AddAddress($q_email->m_email);
					$mail->Subject = QOUTE_MAIL_TITLE;
					$mail->Body = $body;
					$mail->Send();
					
					
					
					$mail = new PHPMailer();
					$mail->IsMail();
					$mail->CharSet="utf-8";
					$mail->IsHTML(true);
					$body = '';
					$mail->From = $q_email->m_email;
					$file = APPPATH.'/mail/qoute_sale.txt';
					$handle = fopen($file,'r');
					if ($handle) {
					    while (!feof($handle)) {
						$file_ = fgets($handle, 4096);
						$body .= $file_."<br>";
					    }
					    fclose($handle);
					}
					$body = str_replace('{NAME}',$q_email->m_name,$body);
					$body = str_replace('{LASTNAME}',$q_email->m_lastname,$body);
					$body = str_replace('{ORDER_TABLE}',$mailProductTable,$body);
					$mail->FromName = SALE_QOUTE_MAIL_TITLE;
					$mail->AddReplyTo($q_email->m_email, SALE_QOUTE_MAIL_TITLE);
					$mail->AddAddress(MAIN_EMAIL);
					$mail->Subject = SALE_QOUTE_MAIL_TITLE;
					$mail->Body = $body;
					$mail->Send();
					/******************** SEND MAIL *********************/
					
					$data['msg'] .= $status_msg['qoute_success'];
					$data['status'] = 'success';
				}				
			}
		//}else{
		//
		//}
		
		echo json_encode($data);
	}
?>